home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / NoCompose.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  46 lines

  1. //: C21:NoCompose.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Writing out the function objects explicitly
  7. #include "copy_if.h"
  8. #include <algorithm>
  9. #include <vector>
  10. #include <string>
  11. #include <iostream>
  12. #include <functional>
  13. #include <cstdlib>
  14. #include <ctime>
  15. using namespace std;
  16.  
  17. class Rgen {
  18.   const int max;
  19. public:
  20.   Rgen(int mx = 100) : max(RAND_MAX/mx) {
  21.     srand(time(0));
  22.   }
  23.   int operator()() { return rand() / max; }
  24. };
  25.  
  26. class BoundTest {
  27.   int top, bottom;
  28. public:
  29.   BoundTest(int b, int t) : bottom(b), top(t) {}
  30.   bool operator()(int arg) {
  31.     return (arg >= bottom) && (arg <= top);
  32.   }
  33. };
  34.  
  35. int main() {
  36.   vector<int> v(100);
  37.   generate(v.begin(), v.end(), Rgen());
  38.   vector<int> r;
  39.   copy_if(v.begin(), v.end(), back_inserter(r),
  40.     BoundTest(30, 40));
  41.   sort(r.begin(), r.end());
  42.   copy(r.begin(), r.end(),
  43.     ostream_iterator<int>(cout, " "));
  44.   cout << endl;
  45. } ///:~
  46.